// pages/Goods/goodsOrder/goodsOrderDetail/index.js
const common = require("../../../../public.js")
Page({
    /**
     * 页面的初始数据
     */
    data: {
        moreGood:false,
        orderList: [], //商品数组
        status: 0,
        id: 0,
        orderDes: {},
        code: true,
        storeInfo: {},
        isLoading: false,
        showRefundDialog: false,
        refundRadioValue: '',
        refundReasons: ['不想买了', '拍错了', '卖家缺货', '我不喜欢', '其他'],
        timeFlag: true,
        title: '测试标题'
    },
    // 优惠券明细
    click_discount(){
        // console.log(this.data.orderDes.discountMoney,"打印打印")
        if(this.data.orderDes.discountMoney==0){
            return
        }
        wx.navigateTo({
          url: '/packageC/pages/coupDiscount/coupDiscount',
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    // 查看物流
    click_express() {
        const expressNumber = this.data.orderDes.expressNumber
        const picPath = this.data.orderDes.picPath
        wx.navigateTo({
            url: '/packageB/pages/express/express?expressNumber=' + expressNumber +
                '&storeImage=' + picPath,
        })
    },
    onLoad: function (options) {
        // console.log(options,"88888888")
        var that = this
        this.data.orderId = options.orderId || 879444
    },
    onShow: function () {
        const that = this
        wx.getStorage({
            key: 'user',
            success: function (res) {
                that.setData({
                    userInfo: res.data.user,
                    token: res.data.token
                }, () => {
                    that.getOrderDes();
                })
            }
        })
    },

    // 状态
    fnStatus(status) {
        var statusName = ''
        switch (status) {
            case 1:
                statusName = '未支付'
                break;
            case 2:
                statusName = '已支付'
                break;
            case 4:
                statusName = '已发货'
                break;
            case 5:
                statusName = '交易完成'
                break;
            case 6:
                statusName = '已关闭'
                break;
            case 7:
                statusName = '已退款'
                break;
            default:
                break;
        }
        return statusName

    },
    getOrderDes: function () {
        var that = this;
        this.setData({
            isLoading: true
        })
        common.post('/order/getOrderDetail', {
                userId: that.data.userInfo.id,
                orderId: that.data.orderId,
                token: that.data.token
            },
            function (responseData) {
                if (responseData && responseData.status === 200) {
                    for (let i = 0; i < responseData.data.length; i++) {
                        if (responseData.data[i].orderId == that.data.orderId) {
                            const orderTime = that.time(responseData.data[i].orderCreated)
                            const paymentTime = that.time(responseData.data[i].paymentTime)
                            var time = that.time(responseData.data[i].endTime)
                            var time2 = common.dateFormat('yyyy-MM-dd HH:mm:ss', responseData.data[i].endTime, +7)
                            console.log(time2, "885554366222")
                            let t = new Date(time2).getTime() //t是7天后的
                            const tttt = that.time(t)
                            let tNow = new Date().getTime()
                            const nowT = that.time(tNow)

                            if (t - tNow <= 0) {
                                var timeFlag = false //超过
                            } else {
                                var timeFlag = true
                            }
                            // 
                            var title = that.fnStatus(responseData.data[i].status)
                            console.log(responseData.data[i], "cecedsdllllll")
                            that.setData({
                                finalPayMent:parseFloat(responseData.data[0].payment) + parseFloat(responseData.data[0].postFee),
                                // moreGood: responseData.data.length > 2 ? true : false,
                                orderList: responseData.data,
                                orderDes: responseData.data[i], //订单信息
                                orderTime: orderTime,
                                paymentTime: paymentTime,
                                timeFlag, //超过 7 天后就是false
                                title
                            }, () => {
                                wx.setNavigationBarTitle({
                                    title: that.data.title, //页面标题为路由参数
                                })
                            })
                        }
                        // 获取店铺信息
                        that.getStoreInfo(responseData.data[i].storeId)
                        break
                    }
                } else {
                    common.showRequestErrMsg(responseData)
                }
            },
            function () {
                that.setData({
                    isLoading: false
                })
            }
        )
    },



    getStoreInfo(storeId) {
        common.post('/store/info', {
                storeId
            },
            (res) => {
                this.setData({
                    storeInfo: res.data
                })
            })
    },
    time(time) {
        if (!time) {
            return '-'
        }
        return common.dateFormat('yyyy-MM-dd HH:mm:ss', time)
    },
    look(e) {
        const status = this.data.orderDes.status
        if (status == 2 || status == 4 || status == 5) {
            const url = '/packageA/pages/Goods/goodsOrder/goodsOrderQRCode/index?title=' + this.data.orderDes.title +
                '&orderNo=' + this.data.orderDes.orderNo +
                '&orderTime=' + this.data.orderTime +
                '&paymentTime=' + this.data.paymentTime +
                '&address=' + (this.data.storeInfo.address ? this.data.storeInfo.address : '') +
                '&mobile=' + (this.data.storeInfo.mobile ? this.data.storeInfo.mobile : '') +
                '&orderId=' + this.data.orderId +
                '&userId=' + this.data.userInfo.id
            wx.navigateTo({
                url
            })
        } else {
            common.showToast('该订单不可查看二维码')
        }
    },
    close(e) {
        this.setData({
            code: true
        })
    },
    contact() {
        const mobile = this.data.storeInfo.mobile
        wx.makePhoneCall({
            phoneNumber: mobile
        })
    },
    gotoGoodsetail() {
        const idx = this.data.orderDes.itemId
        const title = this.data.orderDes.title
        const image = this.data.orderDes.picPath
        console.log(idx);
        wx.navigateTo({
            url: '/packageA/pages/Goods/GoodsDes/index?id=' + idx + '&name=' + title + '&image=' + image,
        })
    },
    showPostInfo() {

    },
    toStore(e) {
        common.gotoStore(this.data.storeInfo)
    },
    refund() {
        this.setData({
            showRefundDialog: true
        })
    },
    commitRefund() {
        console.log(this.data.refundRadioValue)
        if (this.data.refundRadioValue === '') {
            common.showToast('请选择退款原因')
            return
        }
        const that = this
        const param = {
            userId: that.data.userInfo.id,
            orderId: that.data.orderId,
            token: that.data.token,
            orderNo: this.data.orderDes.orderNo,
            orderItemId: this.data.orderDes.id,
            refundReason: this.data.refundReasons[this.data.refundRadioValue],
            refundAmount: this.data.orderDes.totalFee
        }
        common.post('/refund/order',
            param,
            function (res) {
                if (res.status === 200) {
                    wx.redirectTo({
                        url: '/packageA/pages/Goods/goodsOrder/goodsOrderRefund/index',
                    })
                } else {
                    common.showRequestErrMsg(res)
                }
            }
        )
    },
    onClose() {
        this.setData({
            showRefundDialog: false,
            refundRadioValue: ''
        })
    },
    onChange(e) {
        console.log(e.detail)
        this.setData({
            refundRadioValue: e.detail,
        })
    },
       // 查看更多
       click_lenght() {
        this.setData({
            moreGood: false
        })
    },
})